#!/usr/bin/env bash

lower_upper()
{
    local s="Hello World"
    echo $s | tr [:lower:] [:upper:]
    echo ${s^^}
    echo ${s,,}
    echo ${s~~}
}

match()
{
    local split="------------------------------"

    local var="hello eworld"
    echo $var
    echo ${var#h*e}
    echo ${var##h*e}
    echo $var

    echo $split

    var="hello we are world"
    echo $var
    echo ${var%w*d}
    echo ${var%%w*d}

    echo $split

    var="hello we are world we are not world"
    echo $var
    echo ${var/we are}
    echo ${var//we are}

    echo $split

    local version=$(openssl version)
    echo $version
    local check_version=1.0.1
    if [[ "${version/*$check_version*/YES}" == YES ]]
    then
        echo "is $check_version"
    else
        echo "not $check_version"
    fi
    if [[ $version =~ $check_version ]]
    then
        echo "is $check_version"
    else
        echo "not $check_version"
    fi
}

offset()
{
    local split="------------------------------"

    local file="__hello.txt"
    if [[ ${file:0:2} == '__' ]]
    then
        echo "find $file"
    fi
# offset为负时，与前面:之间需要空格
# 最后4位
    local post=${file: -4:4}
    echo $post
    post=${file: -4}
    echo $post
# length为负时，表示下标
    post=${file: -4:-1}
    echo $post
    post=${file:3:-1}
    echo $post
}

replace()
{
    local split="------------------------------"

    local def="hello"
    local option=${option:-$def}
    echo $option

    LANG=C
    local addr=$(/sbin/ifconfig eth0 | grep "inet addr" | grep -v grep)
    addr=${addr%  Bcast*}
    echo "($addr)"
    addr=${addr#*:}
    echo "($addr)"
}

sed_op()
{
    local split="------------------------------"

# 选出数字
    local s="xxx 12 34 sadfa 5678 xxx"
    echo "original: $s"
    echo -n "digits: "
    echo $s | sed 's/[^0-9]*\([0-9][0-9]*\)[^0-9]*/\(\1\)/g'
}

xml()
{
    local split="------------------------------"

# xml中选取一个属性
    local xml=$(cat <<EOF
    <?xml version="1.0"?>
    <info>
        <entry kind="dir" path="xxx" revision = "249" tag="1">
            <url>http://www.xxx.com/yyy</url>
            <repository>
                <root></root>
                <uuid>a086ec9a-93cf-e011-9f7a-68b599af6a42</uuid>
            </repository>
            <commit revision="249">
                <author>arron</author>
                <date>2012-01-30T08:23:50.052378Z</date>
            </commit>
        </entry>
    </info>
EOF
    )
    echo $xml
    local revision=$xml
    revision=${revision#*<info>}
    revision=${revision%%</info>*}
    revision=${revision#*<entry}
    revision=${revision%%>*}
    revision=${revision#*revision}
    revision=${revision#*\"}
    revision=${revision%%\"*}
    echo "info.entry.revision: $revision"
}

main()
{
    local split="=================================================="
    echo $split

    echo "##### 大小写转换 #####"
    echo
    lower_upper

    echo $split

    echo "##### 匹配 #####"
    echo
    match

    echo $split

    echo "##### 偏移 #####"
    echo
    offset

    echo $split

    echo "##### 替换 #####"
    echo
    replace

    echo $split

    echo "##### sed #####"
    echo
    sed_op

    echo $split

    echo "##### xml #####"
    echo
    xml

    echo $split
}

main
exit $?
